package bom.example.common.excel.controller;

import bom.example.common.common.Results;
import bom.example.common.excel.domain.ExportTestDatasResp;
import bom.example.common.excel.domain.ImportTestDatasReq;
import bom.example.common.excel.utils.EasyExcelUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author X.I.O
 * @title: ExcelController
 * @projectName springboot
 * @description: TODO
 * @date 2021/10/31 13:23
 */
@Slf4j
@Api(tags = "Excel文件操作管理")
@RestController
@RequestMapping("/excel")
public class ExcelController {

    /**
     * <p> MultipartFile 这个类一般是用来接收前台传过来的文件信息 </p >
     */
    @ApiOperation(value = "导入XXX数据")
    @PostMapping("/importTestDatas")
    public JSONObject importTestDatas(@RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {

        List<ImportTestDatasReq> ImportTestDatasReqList = EasyExcelUtil.readExcel(multipartFile.getInputStream(), ImportTestDatasReq.class, new EasyExcelUtil.ExcelListener<>());
        ImportTestDatasReqList.forEach(System.out::println);
        return Results.success();
    }

    @ApiOperation(value = "导出XXX数据")
    @GetMapping("/exportTestDatas")
    public void exportTestDatas(HttpServletResponse response) throws IOException, IllegalAccessException, NoSuchFieldException {
        List<ExportTestDatasResp> exportDatas = new ArrayList<>();
        exportDatas.add(new ExportTestDatasResp("王五","广州",new BigDecimal(15000),"0",null,new Date(),null));
        exportDatas.add(new ExportTestDatasResp("赵六","深圳",new BigDecimal(18000),"1",null,new Date(), null));
        EasyExcelUtil.writeEasyExcel(response, ExportTestDatasResp.class, exportDatas, "测试数据");
    }
}
